import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;

import java.util.Arrays;
import java.util.List;

/**
 * @Auther: tianchao
 * @Date: 2021/11/26 23:29
 * @Description:
 */
public class LuaTest {
    public static void main(String[] args) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMinIdle(5);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.10.200", 6379, 3000, null);
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.set("product_stock_10086", "15");
            String script = " local count = redis.call('get',KEYS[1]) " +
                            " local a = tonumber(count) "+
                            " local b = tonumber(ARGV[1]) " +
                            " if a>=b then "+
                            "   redis.call('set',KEYS[1],a-b) "+
                            "   data = 1 " +
                            "   return 1 " +
                            " end " +
                            " return 0 ";
            Object product_stock_10086 = jedis.eval(script, Arrays.asList("product_stock_10086"), Arrays.asList("15"));
            System.out.println(product_stock_10086 + " :  "+ product_stock_10086.getClass());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
